package com.wang.socket;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.wang.auto.addSet.AddSetAutoSend;
import com.wang.auto.addSet.AddSetControlUtil;
import com.wang.conf.AppConf;
import com.wang.conf.AppData;
import com.wang.conf.DevLogEnum;
import com.wang.conf.DevManager;
import com.wang.packpojo.*;
import com.wang.pojo.*;
import com.wang.service.*;
import com.wang.util.*;
import com.wang.webSocket.WebSocketUtil;
import com.wang.wxpojo.*;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.SneakyThrows;

import javax.crypto.spec.PSource;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.InetSocketAddress;
import java.text.ParseException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;

@ChannelHandler.Sharable
public class InBoundHandler extends SimpleChannelInboundHandler<String> {


    private Gson gson = new Gson();
    private String heart = "METER";

    //开始连接
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        super.channelActive(ctx);

        ConMsg conMsg = new ConMsg();
        conMsg.setConnectTime(new Date().getTime());
        conMsg.setCtx(ctx);

        SocketMap.notRegDeviceMap.put(getRemoteAddress(ctx), conMsg);
        PrintUtil.print("设备：" + getRemoteAddress(ctx) + "开始连接");
    }


    @Override
    public void channelRead0(ChannelHandlerContext ctx, String data) throws Exception {


        //连接的通道地址
        String address = getRemoteAddress(ctx);
        //发送测试端数据
        //如果已注册设备中包含 只处理心跳包 以及数据  如果没有 只处理注册包、以及心跳数据 只有在设备有的情况下
        if (SocketMap.mRegPassDeviceMap != null && SocketMap.mRegPassDeviceMap.containsKey(address)) {


            ConMsg conMsg = SocketMap.mRegPassDeviceMap.get(address);

            //更新心跳包
            conMsg.getConHeart().setReceiveTime(new Date());
            Device device = conMsg.getDevice();
            WebSocketUtil.sendTestData(device.getDevGId(), data);
        }
        //如果已注册设备中包含
        if (SocketMap.jRegPassDeviceMap != null && SocketMap.jRegPassDeviceMap.containsKey(address)) {

            ConMsg conMsg = SocketMap.jRegPassDeviceMap.get(address);
            conMsg.getConHeart().setReceiveTime(new Date());
            Device device = conMsg.getDevice();
            WebSocketUtil.sendTestData(device.getDevGId(), data);
        }
        //如果已注册设备中包含
        if (SocketMap.c1RegPassDeviceMap != null && SocketMap.c1RegPassDeviceMap.containsKey(address)) {

            ConMsg conMsg = SocketMap.c1RegPassDeviceMap.get(address);
            conMsg.getConHeart().setReceiveTime(new Date());
            Device device = conMsg.getDevice();
            WebSocketUtil.sendTestData(device.getDevGId(), data);
        }
        //如果已注册设备中包含
        if (SocketMap.c2RegPassDeviceMap != null && SocketMap.c2RegPassDeviceMap.containsKey(address)) {

            ConMsg conMsg = SocketMap.c2RegPassDeviceMap.get(address);
            conMsg.getConHeart().setReceiveTime(new Date());
            Device device = conMsg.getDevice();
            WebSocketUtil.sendTestData(device.getDevGId(), data);
        }

        //继续判断是未注册的设备的 注册包
        if (data.contains("DSDEVREG")) {

            //判断是json设备  还是modbus 设备
            //获取注册包设备编号
            String[] datas = data.split(":");
            String devGId = datas[1];

            regDevice(devGId, ctx);

            return;
        }

        //对数据类型进行判断 ，如果是json数据 调用json处理方法 ,如果是modbus数据，调用其他方法
        //第二次判断是否是哪一种设备
        if (SocketMap.jRegPassDeviceMap.containsKey(address)) {
            //调用json 处理方法
            jDataHander(data, ctx);
            return;
        }
        if (SocketMap.mRegPassDeviceMap.containsKey(address)) {

            mDataHander(data, ctx);
            return;
        }
        //自定义1数据格式
        if (SocketMap.c1RegPassDeviceMap.containsKey(address)) {

            c1DataHander(data, ctx);
            return;
        }
        //自定义1数据格式
        if (SocketMap.c2RegPassDeviceMap.containsKey(address)) {

            c2DataHander(data, ctx);
            return;
        }


    }

    //连接异常
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        //断开连接
        String address = getRemoteAddress(ctx);
        PrintUtil.print(getRemoteAddress(ctx) + "断开连接");
        //断开连接
        disConnectDev(address);

    }


    //异常断开 和正常断开socket  清除连接
    public void disConnectDev(String address) throws ParseException {

        PrintUtil.print("断开：" + address);
        //modbus设备
        if (SocketMap.mRegPassDeviceMap != null && SocketMap.mRegPassDeviceMap.containsKey(address)) {
            ConMsg conMsg = SocketMap.mRegPassDeviceMap.get(address);
            conMsg.getCtx().close();


            //设置设备离线
            Device device = conMsg.getDevice();

            DevStatusUtil.handerOfflineEvent(device.getDevId());

            PrintUtil.print("设备：" + device.getDevGId() + " 设置了离线");

            SocketMap.mRegPassDeviceMap.remove(address);
            //存储设备运行日志
            DevLog devLog = new DevLog();
            devLog.setDevId(device.getDevId().toString());
            devLog.setDevLogTypeCode(AppData.devRunLogCode);
            devLog.setDevEventTypeCode(AppData.devOffLineCode);
            String msg = "设备离线";
            devLog.setDevLogContent(msg);
            devLog.setCreateTime(DateUtil.getStrDate());
            devLog.setOperateUser("system");
            devLog.setIsRead(1);
            //存储
            DevLogService devLogService = SpringUtil.getBean("devLogServiceImpl", DevLogServiceImpl.class);
            devLogService.addDevLog(devLog);
            //存储设备异常日志
            devLog.setDevLogTypeCode(AppData.devExceptionLogCode);
            devLogService.addDevLog(devLog);


        }
        //json设备
        if (SocketMap.jRegPassDeviceMap != null && SocketMap.jRegPassDeviceMap.containsKey(address)) {
            ConMsg conMsg = SocketMap.jRegPassDeviceMap.get(address);
            conMsg.getCtx().close();


            //设置设备离线
            Device device = conMsg.getDevice();
            DevStatusUtil.handerOfflineEvent(device.getDevId());

            PrintUtil.print("设备：" + device.getDevGId() + " 设置了离线");

            SocketMap.jRegPassDeviceMap.remove(address);
            //存储日志
            DevLog devLog = new DevLog();
            devLog.setDevId(device.getDevId().toString());
            devLog.setDevLogTypeCode(AppData.devRunLogCode);
            devLog.setDevEventTypeCode(AppData.devOffLineCode);
            String msg = "设备离线";
            devLog.setDevLogContent(msg);
            devLog.setCreateTime(DateUtil.getStrDate());
            devLog.setOperateUser("system");
            devLog.setIsRead(1);
            //存储
            DevLogService devLogService = SpringUtil.getBean("devLogServiceImpl", DevLogServiceImpl.class);
            devLogService.addDevLog(devLog);
            //存储设备异常日志
            devLog.setDevLogTypeCode(AppData.devExceptionLogCode);
            devLogService.addDevLog(devLog);


        }


        if (SocketMap.notRegDeviceMap != null && SocketMap.notRegDeviceMap.containsKey(address)) {
            ConMsg conMsg = SocketMap.notRegDeviceMap.get(address);
            conMsg.getCtx().close();
            SocketMap.notRegDeviceMap.remove(address);
        }
        if (SocketMap.regFailDeviceMap != null && SocketMap.regFailDeviceMap.containsKey(address)) {
            ConMsg conMsg = SocketMap.regFailDeviceMap.get(address);
            conMsg.getCtx().close();
            SocketMap.regFailDeviceMap.remove(address);
        }


    }


    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) {
        ctx.flush();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        cause.printStackTrace();
        PrintUtil.print(getRemoteAddress(ctx) + "异常通信,断开连接");
        ctx.close();
    }




    public void regDevice(String devGId, ChannelHandlerContext ctx) throws ParseException {

        //判断是json设备  还是modbus 设备
        //获取注册包设备编号
        //查询该设备是json  还是modbus
        ConMsg conMsg = new ConMsg();

        String address = getRemoteAddress(ctx);
        if (SocketMap.deviceMap.containsKey(devGId)) {
            Device device = SocketMap.deviceMap.get(devGId);


            conMsg.setDevice(device);
            conMsg.setCtx(ctx);
            //添加心跳包
            ConHeart conHeart = new ConHeart();
            conMsg.setConHeart(conHeart);
            conMsg.setConnectTime(new Date().getTime());
            //设置注册时间
            conMsg.setRegTime(new Date().getTime());
            //设置数据传输方式
            conMsg.setTxDataType(device.getTxDataType());


            //采集频率
            DevCollectRateService devCollectRateService = SpringUtil.getBean("devCollectRateServiceImpl", DevCollectRateServiceImpl.class);
            DevCollectRate devCollectRate = devCollectRateService.selectCollectRateByDevId(device.getDevId());
            if(EmptyUtil.isNotNull(devCollectRate.getDevCrValue())){
                conMsg.setCollectRate(Integer.parseInt(devCollectRate.getDevCrValue()));
            }else {
                conMsg.setCollectRate(5);
            }


            //心跳包
            DevConConfigService devConConfigService = SpringUtil.getBean("devConConfigServiceImpl", DevConConfigServiceImpl.class);
            DevConConfig conConfig = devConConfigService.selectConfigByDevId(device.getDevId());

            conMsg.getConHeart().setHeartBeat(conConfig.getDevHeart());

            //数据开关
            DevSwitchService devSwitchService = SpringUtil.getBean("devSwitchServiceImpl", DevSwitchServiceImpl.class);
            DevSwitch devSwitch = devSwitchService.selectByDevId(device.getDevId());


            conMsg.setIsOpen(devSwitch.getDevSwitchValue());


            //如果是modbus 设备

            //添加modbus数据解析配置
            //查询modbus 配置
            if (device.getTxDataType().equals(AppData.ModbusDataCode)) {

                DevMdtParamConfService mdtParamConfService = SpringUtil.getBean("devMdtParamConfServiceImpl", DevMdtParamConfServiceImpl.class);

                List<DevMdtParamConf> mdtParamConfList = mdtParamConfService.selectDevMdtParamConfByDevId(device.getDevId());


                //指令集合
                Map<String, DevMdtParamConf> commandMap = new HashMap<>();
                DevMdtParamConfDataService mdtParamConfDataService = SpringUtil.getBean("devMdtParamConfDataServiceImpl", DevMdtParamConfDataServiceImpl.class);
                for (DevMdtParamConf mdtParamConf : mdtParamConfList) {

                    List<DevMdtParamConfData> mdtParamConfDataList = mdtParamConfDataService.selectDevMdtParamConfDataByConfId(mdtParamConf.getConfId());
                    if (mdtParamConfDataList.size() > 0) {
                        mdtParamConf.setParamConfDataList(mdtParamConfDataList);


                        String command = ModBusUtil.makeUpCommand(mdtParamConf, mdtParamConfDataList);

                        commandMap.put(command, mdtParamConf);
                    }

                }

                conMsg.setCommandMap(commandMap);
                conMsg.setTxConf(mdtParamConfList);

                //防止重复注册
                //如果重复地址  不处理
                if (!SocketMap.mRegPassDeviceMap.containsKey(address)) {

                    for (Map.Entry<String, ConMsg> entry : SocketMap.mRegPassDeviceMap.entrySet()) {
                        if (entry.getValue().getDevice().getDevGId().equals(devGId)) {
                            //移除现有连接
                            SocketMap.mRegPassDeviceMap.remove(entry.getKey());
                            disConnectDev(entry.getKey());
                        }

                    }

                    SocketMap.mRegPassDeviceMap.put(address, conMsg);
                    SocketMap.notRegDeviceMap.remove(address);


                }



            } else if(device.getTxDataType().equals(AppData.JSONDataCode)) {

                //json配置
                //添加json配置
                //查询json配置 目前json 数据配置只配置一个 后面有需求在扩展
                DevJdtParamConfService jdtParamConfService = SpringUtil.getBean("devJdtParamConfServiceImpl", DevJdtParamConfServiceImpl.class);
                DevJdtParamConf jdtParamConf = jdtParamConfService.selectDevJdtParamConfByDevId(device.getDevId());
                //查询具体参数配置
                DevJdtParamConfDataService jdtParamConfDataService = SpringUtil.getBean("devJdtParamConfDataServiceImpl", DevJdtParamConfDataServiceImpl.class);

                List<DevJdtParamConfData> jdtParamConfDataList = jdtParamConfDataService.selectDevJdtParamConfDataByConfId(jdtParamConf.getConfId());

                jdtParamConf.setParamConfDataList(jdtParamConfDataList);
                //设置传输配置
                conMsg.setTxConf(jdtParamConf);

                //添加json注册成功 全局变量
                if (!SocketMap.jRegPassDeviceMap.containsKey(address)) {

                    for (Map.Entry<String, ConMsg> entry : SocketMap.jRegPassDeviceMap.entrySet()) {
                        if (entry.getValue().getDevice().getDevGId().equals(devGId)) {
                            //移除现有连接
                            SocketMap.jRegPassDeviceMap.remove(entry.getKey());
                            disConnectDev(entry.getKey());
                        }

                    }
                    SocketMap.jRegPassDeviceMap.put(address, conMsg);
                    SocketMap.notRegDeviceMap.remove(address);
                }


            }else if(device.getTxDataType().equals(AppData.CustomOneCode)) {

                DevC1dtParamConfService devC1dtParamConfService = SpringUtil.getBean("devC1dtParamConfServiceImpl", DevC1dtParamConfServiceImpl.class);

                DevC1dtParamConf devC1dtParamConf = devC1dtParamConfService.selectDevC1dtParamConfByDevId(device.getDevId());


                //指令集合
                Map<String, DevC1dtParamConfData> commandMap = new HashMap<>();
                DevC1dtParamConfDataService c1dtParamConfDataService = SpringUtil.getBean("devC1dtParamConfDataServiceImpl", DevC1dtParamConfDataServiceImpl.class);


                List<DevC1dtParamConfData> devC1dtParamConfDataList = c1dtParamConfDataService.selectC1dtParamConfDataByConfId(devC1dtParamConf.getConfId());
                if (devC1dtParamConfDataList.size() > 0) {

                    devC1dtParamConf.setDevC1dtParamConfDataList(devC1dtParamConfDataList);

                    devC1dtParamConfDataList.forEach(devC1dtParamConfData ->{

                        commandMap.put(devC1dtParamConfData.getCommand(), devC1dtParamConfData);
                    });


                }



                conMsg.setCommandMap(commandMap);
                conMsg.setTxConf(devC1dtParamConf);

                //防止重复注册
                //如果重复地址  不处理
                if (!SocketMap.c1RegPassDeviceMap.containsKey(address)) {

                    for (Map.Entry<String, ConMsg> entry : SocketMap.c1RegPassDeviceMap.entrySet()) {
                        if (entry.getValue().getDevice().getDevGId().equals(devGId)) {
                            //移除现有连接
                            SocketMap.c1RegPassDeviceMap.remove(entry.getKey());
                            disConnectDev(entry.getKey());
                        }

                    }

                    SocketMap.c1RegPassDeviceMap.put(address, conMsg);
                    SocketMap.notRegDeviceMap.remove(address);


                }

            } else if(device.getTxDataType().equals(AppData.CustomTWOCode)) {


/*
                //指令集合
                Map<String, String> commandMap = new HashMap<>();


                String command = "01 03 00 00 00 08 44 0C";
                commandMap.put(command,command);*/


              /*  conMsg.setCommandMap(commandMap);
                conMsg.setTxConf(command);*/

                //防止重复注册
                //如果重复地址  不处理
                if (!SocketMap.c2RegPassDeviceMap.containsKey(address)) {

                    for (Map.Entry<String, ConMsg> entry : SocketMap.c2RegPassDeviceMap.entrySet()) {
                        if (entry.getValue().getDevice().getDevGId().equals(devGId)) {
                            //移除现有连接
                            SocketMap.c2RegPassDeviceMap.remove(entry.getKey());
                            disConnectDev(entry.getKey());
                        }

                    }

                    SocketMap.c2RegPassDeviceMap.put(address, conMsg);
                    SocketMap.notRegDeviceMap.remove(address);


                }

            }
            PrintUtil.print("注册连接：" + devGId);


            //设置设备在线时间处理 更新状态
            DevStatusUtil.handerOnlineEvent(device.getDevId());
            PrintUtil.print("设备：" + device.getDevGId() + " 设置了在线");

            //存储日志
            DevLog devLog = new DevLog();
            devLog.setDevId(device.getDevId().toString());
            devLog.setDevLogTypeCode(AppData.devRunLogCode);
            devLog.setDevEventTypeCode(AppData.devOnlineLogCode);
            String msg = "设备上线";
            devLog.setDevLogContent(msg);
            devLog.setCreateTime(DateUtil.getStrDate());
            devLog.setOperateUser("system");
            devLog.setIsRead(1);
            //存储
            DevLogService devLogService = SpringUtil.getBean("devLogServiceImpl", DevLogServiceImpl.class);
            devLogService.addDevLog(devLog);
            //例外处理
            //如果是设备LH-DZ_93Y48620F  注册 自动校准开关数据库状态
            if(devGId.equals("LH-DZ_93Y48620F")){
                try {
                    DevControlUtil.correctSwitchStatus(ctx,devGId);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            //例外处理2
            //如果设备LH-DZ_98S343C95 注册 自动设置空调状态为开
            if(devGId.equals("LH-DZ_98S343C95")){
                DevControlUtil.setStartStatus(devGId);
            }
            //例外处理3  设备附加设置
            if(AddSetAutoSend.addSetDevMap.containsKey(devGId)){
                /*//空调默认开启
                DevControlUtil.setStartStatus(devGId);*/
                try {
                    DevControlUtil.correctSwitchStatus(ctx,devGId);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }





        } else {
            notRegHander(devGId, ctx);
            PrintUtil.print(devGId + ":设备不存在");
        }


    }


    //未注册设备处理方法
    public void notRegHander(String devGId, ChannelHandlerContext ctx) {


        Device device = new Device();
        device.setDevGId(devGId);
        ConMsg conMsg = new ConMsg();
        //添加心跳包
        ConHeart conHeart = new ConHeart();
        conMsg.setConHeart(conHeart);
        conMsg.setDevice(device);
        conMsg.setCtx(ctx);
        //更新连接时间
        conMsg.setConnectTime(new Date().getTime());
        String address = getRemoteAddress(ctx);

        try {
            SocketMap.notRegDeviceMap.put(address, conMsg);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    //modbus 数据处理方法
    public void mDataHander(String mData, ChannelHandlerContext ctx) throws ParseException {


        //连接的通道地址
        String address = getRemoteAddress(ctx);


        //如果已注册设备中包含 只处理心跳包 以及数据  如果没有 只处理注册包、以及心跳数据 只有在设备有的情况下
        if (SocketMap.mRegPassDeviceMap != null && SocketMap.mRegPassDeviceMap.containsKey(address)) {

            ConMsg conMsg = SocketMap.mRegPassDeviceMap.get(address);
            Device device = conMsg.getDevice();


            if (mData.equals(heart)) { //心跳包 从注册设备中寻找设备
                PrintUtil.print("心跳包：" + device.getDevGId() + ":" + mData);

                //1、先判断该设备是否已经注册
                if (SocketMap.mRegPassDeviceMap != null && SocketMap.mRegPassDeviceMap.containsKey(address)) {

                    //设置心跳包收到的时间
                    SocketMap.mRegPassDeviceMap.get(address).getConHeart().setReceiveTime(new Date());
                    PrintUtil.print("更新设备心跳包：" + device.getDevGId());
                    if (SocketMap.deviceMap != null && SocketMap.deviceMap.containsKey(device.getDevGId())) {
                        //判断全局变量中的状态  如果全局变量中是离线更新数据库
                        Device device1 = SocketMap.deviceMap.get(device.getDevGId());
                        //如果目前状态是离线 ，更新为在线
                        if (device1.getDevStatusCode().equals(AppData.offLineCode)) {
                            device1.setDevStatusCode(AppData.onLineCode);
                            //更新数据库设备状态
                            DevStatusUtil.handerOnlineEvent(device.getDevId());
                        }
                    }
                }
            } else {

                //数据命令
                //解析数据
                PrintUtil.print("数据：" + device.getDevGId() + ":" + mData);
                String strData = mData.replaceAll(" ", "");
                byte[] bytesData = ModBusUtil.HexString2Bytes(strData);

                //获取功能码
                String funCode = mData.split(" ")[1];
                if(!funCode.equals("03") && !funCode.equals("04")){
                    //暂时没用 放到编码器中处理
                    //不等于03 按其他指令处理
                    //从全局变量中查询是否包含此功能码地址
                    if(AppData.returnMap !=null && AppData.returnMap.get(device.getDevGId())!=null && AppData.returnMap.get(device.getDevGId()).containsKey(funCode)){

                        Map<String, Object> resultMap = AppData.returnMap.get(device.getDevGId());

                        if (resultMap.containsKey(funCode)) {
                            resultMap.put(funCode, strData);
                        }
                        WebSocketUtil.sendTestData(device.getDevGId(), mData);
                    }

                }else {
                    //收发数据
                    if (SocketMap.mRegPassDeviceMap != null && SocketMap.mRegPassDeviceMap.size() > 0 && SocketMap.mRegPassDeviceMap.containsKey(address)) {
                        //判断全局变量中设备是否在线，如果不在线 更新全局变量状态以及数据库状态
                        if (SocketMap.deviceMap != null && SocketMap.deviceMap.containsKey(device.getDevGId())) {
                            if (SocketMap.deviceMap.get(device.getDevGId()).getDevStatusCode().equals(AppData.offLineCode)) {
                                SocketMap.deviceMap.get(device.getDevGId()).setDevStatusCode(AppData.onLineCode);
                                //更新数据库状态
                                DevStatusUtil.handerOnlineEvent(device.getDevId());
                            }
                        }
                        //1、判断设备是单个指令  还是多个指令
                        //1.1、如果是单个指令 直接存储数据
                        Map<String, DevMdtParamConf> commandMap = conMsg.getCommandMap();
                        //1.1、如果是单个指令 直接存储数据
                        if (commandMap != null && commandMap.size() == 1) {

                            if (conMsg.getNowCommandPack()!=null && conMsg.getNowCommandPack().getParamConf()!=null) {
                                DevMdtParamConf mdtParamConf = (DevMdtParamConf) conMsg.getNowCommandPack().getParamConf();
                                List<Param> paramList = ModBusUtil.readData(mdtParamConf, mdtParamConf.getParamConfDataList(), bytesData);
                                //存储数据
                                saveData(device, paramList, DateUtil.getStrDate());
                            }else {
                                PrintUtil.print("错误日志："+device.getDevGId()+"找不到配置");
                            }
                        } else {
                            //1.2、多个指令

                            // 判断数据位地址 来判断 哪个指令
                            //获取modbus协议 地址位

                            //截取前两位
                            String addressCode = mData.substring(0, 2);


                            DevMdtParamConf mdtParamConf1 = null;
                            if (conMsg.getCommandMap().size()>0) {

                                //1、首先判断当前命令是否与数据地址位是否相等
                                NowCommandPack nowCommandPack = conMsg.getNowCommandPack();

                                //相等
                                if (nowCommandPack!=null && nowCommandPack.getParamConf()!=null ) {

                                    DevMdtParamConf devMdtParamConf = (DevMdtParamConf) nowCommandPack.getParamConf();
                                        if(devMdtParamConf.getAddressCode().equals(addressCode)){
                                            mdtParamConf1 = devMdtParamConf;
                                        }else {
                                            //不相等
                                            for(Map.Entry<String, DevMdtParamConf> entry : commandMap.entrySet()){
                                                String command = entry.getKey();

                                                String commandAddressCode = command.split(" ")[0];

                                                if(commandAddressCode.equals(addressCode)){
                                                    mdtParamConf1 = entry.getValue();
                                                    break;
                                                }

                                            }
                                        }

                                }else {

                                    //当前指令被清空
                                   for(Map.Entry<String, DevMdtParamConf> entry : commandMap.entrySet()){
                                       String command = entry.getKey();
                                       String commandAddressCode = command.split(" ")[0];
                                       if(commandAddressCode.equals(addressCode)){
                                           mdtParamConf1 = entry.getValue();
                                           break;
                                       }

                                   }

                                }

                            }

                            if (mdtParamConf1 != null) {
                                List<DevMdtParamConfData> mdtParamConfDataList = mdtParamConf1.getParamConfDataList();

                                //判断全局变量中是否存在该设备
                                boolean isExist = SocketMap.mMoreDevDataMap.containsKey(device.getDevGId());
                                if(isExist){
                                    //判断接收数据的时间是否大于正常时间
                                    SaveData saveData = SocketMap.mMoreDevDataMap.get(device.getDevGId());
                                    Long nowTime = new Date().getTime();

                                   Long intervalTime  =  conMsg.getCommandMap().size()* AppConf.commandInterval+1000L;
                                    if(saveData.getReceiveTime() == 0 || nowTime - saveData.getReceiveTime() >= intervalTime){
                                        //清除数据
                                        SocketMap.mMoreDevDataMap.remove(device.getDevGId());
                                        isExist = false;

                                    }
                                }
                                List<Param> paramList = ModBusUtil.readData(mdtParamConf1, mdtParamConfDataList, bytesData);
                                if (isExist) {
                                    if (paramList != null) {
                                        PrintUtil.print("设备："+device.getDevGId()+"开始增加数据存储");
                                        SocketMap.mMoreDevDataMap.get(device.getDevGId()).getParamList().addAll(paramList);
                                        paramList.forEach(param -> {
                                            PrintUtil.print(param.getParamName()+param.getParamValue());
                                        });

                                    }

                                } else {
                                    if (paramList != null) {
                                        PrintUtil.print("设备："+device.getDevGId()+"开始新建数据存储");
                                        paramList.forEach(param -> {
                                            PrintUtil.print(param.getParamName()+param.getParamValue());
                                        });
                                        //新建 存储全局变量
                                        Long currentTime = new Date().getTime();
                                        Date date = DateUtil.longToDate(currentTime);
                                        String strDate = DateUtil.dateToString(date,"yyyy-MM-dd HH:mm:ss");
                                        SaveData saveData = new SaveData();
                                        saveData.setReceiveTime(currentTime);
                                        saveData.setParamList(paramList);
                                        SocketMap.mMoreDevDataMap.put(device.getDevGId(), saveData);
                                        //新建定时任务
                                        mDelayedSave(device, conMsg.getCommandMap().size(),strDate);

                                    }

                                }
                            }
                        }

                        //执行完 清空 传输配置项中当前发送的指令
                        conMsg.setNowCommandPack(null);


                    }
                }
            }

        } else {
            PrintUtil.print("非法设备");
        }

    }
    //custom1 数据处理方法
    public void c1DataHander(String c1Data, ChannelHandlerContext ctx) throws ParseException {


        //连接的通道地址
        String address = getRemoteAddress(ctx);


        //如果已注册设备中包含 只处理心跳包 以及数据  如果没有 只处理注册包、以及心跳数据 只有在设备有的情况下
        if (SocketMap.c1RegPassDeviceMap != null && SocketMap.c1RegPassDeviceMap.containsKey(address)) {

            ConMsg conMsg = SocketMap.c1RegPassDeviceMap.get(address);
            Device device = conMsg.getDevice();


            if (c1Data.equals(heart)) { //心跳包 从注册设备中寻找设备
                PrintUtil.print("心跳包：" + device.getDevGId() + ":" + c1Data);

                //1、先判断该设备是否已经注册
                if (SocketMap.c1RegPassDeviceMap != null && SocketMap.c1RegPassDeviceMap.containsKey(address)) {

                    //设置心跳包收到的时间
                    SocketMap.c1RegPassDeviceMap.get(address).getConHeart().setReceiveTime(new Date());
                    PrintUtil.print("更新设备心跳包：" + device.getDevGId());
                    if (SocketMap.deviceMap != null && SocketMap.deviceMap.containsKey(device.getDevGId())) {
                        //判断全局变量中的状态  如果全局变量中是离线更新数据库
                        Device device1 = SocketMap.deviceMap.get(device.getDevGId());
                        //如果目前状态是离线 ，更新为在线
                        if (device1.getDevStatusCode().equals(AppData.offLineCode)) {
                            device1.setDevStatusCode(AppData.onLineCode);
                            //更新数据库设备状态
                            DevStatusUtil.handerOnlineEvent(device.getDevId());
                        }
                    }
                }
            } else {

                //数据命令
                //解析数据
                PrintUtil.print("数据：" + device.getDevGId() + ":" + c1Data);
                String strData = c1Data.replaceAll(" ", "");


                if(strData.indexOf("AA77")==0) {
                    String hexData = "";
                    //完整数据
                    if(strData.length() ==24){
                         hexData = strData.substring(12, 20);
                    }else {
                        //不完整数据   可能是FFFF
                        hexData = strData.substring(12, 16);
                    }

                    String hexCmd = strData.substring(8, 12);


                    Map<String, DevC1dtParamConfData> commandMap = conMsg.getCommandMap();
                    //1.1、如果是单个指令 直接存储数据
                    if (commandMap != null && commandMap.size() == 1) {

                        if (conMsg.getNowCommandPack() != null && conMsg.getNowCommandPack().getParamConf() != null) {
                            DevC1dtParamConfData c1dtParamConfData = (DevC1dtParamConfData) conMsg.getNowCommandPack().getParamConf();

                            List<Param> paramList = new ArrayList<>();
                            Param param = new Param();

                            param.setParamName(c1dtParamConfData.getParamName());
                            if(hexData.equals("0000") || hexData.equals("1111") || hexData.equals("FFFF")){
                                param.setParamValue("--");
                            }else {
                                //转换成float
                               Float fdata =  CustomOneUtil.hex2decimalFloat(hexData);
                               //保留位数
                                BigDecimal bigDecimal = new BigDecimal(fdata);
                                String str = bigDecimal.setScale(c1dtParamConfData.getReserveNum(), BigDecimal.ROUND_DOWN).toString();
                                param.setParamValue(str);
                            }

                            paramList.add(param);
                            //存储数据
                            saveData(device, paramList, DateUtil.getStrDate());
                        } else {
                            PrintUtil.print("错误日志：" + device.getDevGId() + "找不到配置");
                        }
                    } else {
                        //1.2、多个指令

                        // 判断命令参数 来判断 哪个指令

                        DevC1dtParamConfData devC1dtParamConfData1 = null;
                        if (conMsg.getCommandMap().size() > 0) {

                            //1、首先判断当前命令是否与数据地址位是否相等
                            NowCommandPack nowCommandPack = conMsg.getNowCommandPack();

                            //相等
                            if (nowCommandPack != null && nowCommandPack.getParamConf() != null) {

                                DevC1dtParamConfData devC1dtParamConfData = (DevC1dtParamConfData) nowCommandPack.getParamConf();
                                if (devC1dtParamConfData.getCmdData().equals(hexCmd)) {
                                    devC1dtParamConfData1 = devC1dtParamConfData;
                                } else {
                                    //不相等
                                    for (Map.Entry<String, DevC1dtParamConfData> entry : commandMap.entrySet()) {

                                        DevC1dtParamConfData devC1dtParamConfData2 = entry.getValue();
                                        if (devC1dtParamConfData2.equals(hexCmd)) {
                                            devC1dtParamConfData1 = entry.getValue();
                                            break;
                                        }

                                    }
                                }

                            } else {
                                //当前指令被清空
                                for (Map.Entry<String, DevC1dtParamConfData> entry : commandMap.entrySet()) {
                                    DevC1dtParamConfData devC1dtParamConfData2 = entry.getValue();
                                    if (devC1dtParamConfData2.equals(hexCmd)) {
                                        devC1dtParamConfData1 = entry.getValue();
                                        break;
                                    }

                                }

                            }

                        }

                        if (devC1dtParamConfData1 != null) {


                            //判断全局变量中是否存在该设备
                            boolean isExist = SocketMap.c1MoreDevDataMap.containsKey(device.getDevGId());

                            List<Param> paramList = new ArrayList<>();
                            Param param = new Param();
                            param.setParamName(devC1dtParamConfData1.getParamName());
                            if(hexData.equals("0000") || hexData.equals("1111") || hexData.equals("FFFF")){
                                param.setParamValue("--");
                            }else {
                                //转换成float


                                Float fdata =  CustomOneUtil.hex2decimalFloat(hexData);

                                //保留位数
                                BigDecimal bigDecimal = new BigDecimal(fdata);
                                String str = bigDecimal.setScale(devC1dtParamConfData1.getReserveNum(), BigDecimal.ROUND_DOWN).toString();
                                param.setParamValue(str);
                            }


                            paramList.add(param);

                            if (isExist) {
                                if (paramList != null) {
                                    PrintUtil.print("设备：" + device.getDevGId() + "开始增加数据存储");
                                    SocketMap.c1MoreDevDataMap.get(device.getDevGId()).addAll(paramList);

                                }

                            } else {
                                if (paramList != null) {
                                    PrintUtil.print("设备：" + device.getDevGId() + "开始新建数据存储");
                                    //新建 存储全局变量
                                    SocketMap.c1MoreDevDataMap.put(device.getDevGId(), paramList);
                                    //新建定时任务
                                    c1DelayedSave(device, conMsg.getCommandMap().size());

                                }

                            }
                        }
                    }

                    //执行完 清空 传输配置项中当前发送的指令
                    conMsg.setNowCommandPack(null);
                }
                }
            }

        }
    //custom2 数据处理方法
    public void c2DataHander(String c2Data, ChannelHandlerContext ctx) throws ParseException {


        System.out.println("打印数据:"+c2Data);
        //连接的通道地址
        String address = getRemoteAddress(ctx);
        //如果已注册设备中包含 只处理心跳包 以及数据  如果没有 只处理注册包、以及心跳数据 只有在设备有的情况下
        if (SocketMap.c2RegPassDeviceMap != null && SocketMap.c2RegPassDeviceMap.containsKey(address)) {

            ConMsg conMsg = SocketMap.c2RegPassDeviceMap.get(address);
            Device device = conMsg.getDevice();


            if (c2Data.equals(heart)) { //心跳包 从注册设备中寻找设备
                PrintUtil.print("心跳包：" + device.getDevGId() + ":" + c2Data);

                //1、先判断该设备是否已经注册
                if (SocketMap.c2RegPassDeviceMap != null && SocketMap.c2RegPassDeviceMap.containsKey(address)) {

                    //设置心跳包收到的时间
                    SocketMap.c2RegPassDeviceMap.get(address).getConHeart().setReceiveTime(new Date());
                    PrintUtil.print("更新设备心跳包：" + device.getDevGId());
                    if (SocketMap.deviceMap != null && SocketMap.deviceMap.containsKey(device.getDevGId())) {
                        //判断全局变量中的状态  如果全局变量中是离线更新数据库
                        Device device1 = SocketMap.deviceMap.get(device.getDevGId());
                        //如果目前状态是离线 ，更新为在线
                        if (device1.getDevStatusCode().equals(AppData.offLineCode)) {
                            device1.setDevStatusCode(AppData.onLineCode);
                            //更新数据库设备状态
                            DevStatusUtil.handerOnlineEvent(device.getDevId());
                        }
                    }
                }
            } else {

                //数据命令
                //解析数据
                PrintUtil.print("数据：" + device.getDevGId() + ":" + c2Data);
                String strData = c2Data.replaceAll(" ", "");

                    //通过长度判断数据
                    //1、18长度代表读取数据  测量时间+测量值  0000 -0008  0008 没有用
                    //2、 16长度代表读取状态    0100 -0107
                    //3、 10长度代表  写入数据 0201-0204 单独写入
                //获取长度
                String[] data = c2Data.split(" ");
                String funCode  = data[1];
                String lengthCode = data[2];
                Integer length = ModBusUtil.hex2decimal(lengthCode);


                System.out.println("测试功能码："+funCode);

                if(funCode.equals("03")) {
                    //读取数据
                    //读取时间和测量值
                    //1、解析数据
                    if (length.equals(18)) {
                        //读取测量时间和参数值
                        //1、读取年
                        String yearCode = data[3] + data[4];
                        String monthCode = data[5] + data[6];
                        String dayCode = data[7] + data[8];
                        String hourCode = data[9] + data[10];
                        String minuteCode = data[11] + data[12];
                        String secondCode = data[13] + data[14];

                        String paramValueCode =data[17] + data[18]+ data[15] + data[16]  ;


                        Integer year = ModBusUtil.hex2decimal(yearCode);
                        Integer month = ModBusUtil.hex2decimal(monthCode);
                        Integer day = ModBusUtil.hex2decimal(dayCode);
                        Integer hour = ModBusUtil.hex2decimal(hourCode);
                        Integer minute = ModBusUtil.hex2decimal(minuteCode);
                        Integer second = ModBusUtil.hex2decimal(secondCode);

                        String strMonth = month.toString();
                        if(month<10){
                            strMonth = 0+strMonth;
                        }
                        String strDay = day.toString();
                        if(day<10){
                            strDay = 0+strDay;
                        }
                        String strHour = hour.toString();
                        if(hour<10){
                            strHour = 0+strHour;
                        }
                        String strMinute = minute.toString();
                        if(minute<10){
                            strMinute = 0+strMinute;
                        }
                        String strSecond = second.toString();
                        if(second <10){
                            strSecond = 0+strSecond;
                        }



                        String date = year + "-" + strMonth + "-" + strDay + " " + strHour+ ":" + strMinute + ":" + strSecond;

                        Float paramValue = ModBusUtil.hex2decimalFloat(paramValueCode);

                        System.out.println(paramValue);
                        System.out.println(date);

                        //读取参数名称进行赋值
                        DevC2dtParamConfService devC2dtParamConfService = SpringUtil.getBean("devC2dtParamConfServiceImpl", DevC2dtParamConfServiceImpl.class);
                        DevC2dtParamConf devC2dtParamConf = devC2dtParamConfService.selectDevC2dtParamConfByDevId(device.getDevId());
                        DevC2dtParamConfDataService devC2dtParamConfDataService = SpringUtil.getBean("devC2dtParamConfDataServiceImpl", DevC2dtParamConfDataServiceImpl.class);
                        List<DevC2dtParamConfData> devC2dtParamConfDataList = devC2dtParamConfDataService.selectC2dtParamConfDataByConfId(devC2dtParamConf.getConfId());


                        if (devC2dtParamConfDataList.size() > 0) {
                            //只有一个参数
                            DevC2dtParamConfData devC2dtParamConfData = devC2dtParamConfDataList.get(0);
                            Param param = new Param();
                            param.setParamName(devC2dtParamConfData.getParamName());
                            param.setParamValue(String.valueOf(paramValue));
                            List<Param> paramList = new ArrayList<>();
                            paramList.add(param);
                            saveData(device, paramList, date);
                        }

                    } else if (length.equals(16)) {

                        String command = "01 03 01 00 00 08 45 F0";
                        //读取状态
                        if (AppData.returnMap.containsKey(device.getDevGId())) {
                            Map<String, Object> map = AppData.returnMap.get(device.getDevGId());
                            if (map.containsKey(command.replace(" ",""))) {
                                //解析数据
                                String runStatusCode = data[3] + data[4];
                                String warnStatusCode = data[5] + data[6];
                                String yearCode = data[7] + data[8];
                                String monthCode = data[9] + data[10];
                                String dayCode = data[11] + data[12];
                                String hourCode = data[13] + data[14];
                                String minuteCode = data[15] + data[16];
                                String secondCode = data[17] + data[18];



                                Integer year = ModBusUtil.hex2decimal(yearCode);
                                Integer month = ModBusUtil.hex2decimal(monthCode);
                                Integer day = ModBusUtil.hex2decimal(dayCode);
                                Integer hour = ModBusUtil.hex2decimal(hourCode);
                                Integer minute = ModBusUtil.hex2decimal(minuteCode);
                                Integer second = ModBusUtil.hex2decimal(secondCode);
                                String strMonth = month.toString();
                                if(month<10){
                                    strMonth = 0+strMonth;
                                }
                                String strDay = day.toString();
                                if(day<10){
                                    strDay = 0+strDay;
                                }
                                String strHour = hour.toString();
                                if(hour<10){
                                    strHour = 0+strHour;
                                }
                                String strMinute = minute.toString();
                                if(minute<10){
                                    strMinute = 0+strMinute;
                                }
                                String strSecond = second.toString();
                                if(second <10){
                                    strSecond = 0+strSecond;
                                }



                                String date = year + "-" + strMonth + "-" + strDay + " " + strHour+ ":" + strMinute + ":" + strSecond;


                                //组合数据
                                Integer runStatus = ModBusUtil.hex2decimal(runStatusCode);
                                Integer warnStatus = ModBusUtil.hex2decimal(warnStatusCode);
                                DpdStatus dpdStatus = new DpdStatus();
                                dpdStatus.setRunStatus(runStatus);
                                dpdStatus.setWarnStatus(warnStatus);
                                dpdStatus.setPowerOnTime(date);


                                map.put(command, dpdStatus);
                            }

                        }

                    } else if (length.equals(10)) {
                        //读取操作模式
                        String command = "01 03 02 01 00 05 D5 B1";
                        //读取状态
                        if (AppData.returnMap.containsKey(device.getDevGId())) {
                            Map<String, Object> map = AppData.returnMap.get(device.getDevGId());
                            if (map.containsKey(command.replace(" ",""))) {


                                //解析数据
                                String collectModeCode = data[3] + data[4];
                                String collectTimeCode =data[7] + data[8]+ data[5] + data[6];
                                String HourCollectSetCode = data[11] + data[12]+data[9] + data[10];
                                //组合数据
                                Integer collectMode = ModBusUtil.hex2decimal(collectModeCode);
                                Integer collectTime = ModBusUtil.hex2decimal(collectTimeCode);

                                System.out.println(collectMode);

                                Integer hourCollectSet = ModBusUtil.hex2decimal(HourCollectSetCode);
                                DpdMode dpdMode = new DpdMode();
                                dpdMode.setCollectMode(collectMode);
                                dpdMode.setCollectTime(collectTime.intValue());
                                dpdMode.setHourCollectSet(hourCollectSet.intValue());
                                map.put(command, dpdMode);
                            }


                        }

                    }
                }

                if(funCode.equals("06")){


                    //写入值返回
                    //通过判断发送数据与接收数据进行判断返回
                    if(AppData.returnMap.containsKey(device.getDevGId())){

                        Map<String,Object> map = AppData.returnMap.get(device.getDevGId());
                        if(map.size()>0){
                            for(Map.Entry<String,Object> map1 : map.entrySet()){
                                if(map1.getKey().replace(" ","").equals(c2Data.replaceAll(" ",""))){

                                    map1.setValue("设置成功");
                                    break;
                                }
                            }
                        }
                    }

                }
                if(funCode.equals("10")){
                    if(AppData.returnMap.containsKey(device.getDevGId())){

                        Map<String,Object> map = AppData.returnMap.get(device.getDevGId());
                        if(map.size()>0){
                            for(Map.Entry<String,Object> map1 : map.entrySet()){

                                //通过比对前4位进行判断
                                String command1 = map1.getKey();
                                String command2 = c2Data.replaceAll(" ","");
                                String[] d1 = command1.split("");
                                String data1 = d1[0]+d1[1]+d1[2] +d1[3];
                                String[] d2 = command2.split("");
                                String data2 = d2[0]+d2[1]+d2[2] +d2[3];

                                if(data1.equals(data2)){
                                    map1.setValue("设置成功");
                                    break;
                                }
                            }
                        }
                    }
                }


            }
        }

        }




    //modbus定时任务
    public void mDelayedSave(Device device, Integer size,String strDate)  {


       Runnable run =  new Runnable() {
            @SneakyThrows
            public void run() {

                Thread.sleep(size * AppConf.commandInterval+1000);
                //从全局变量中获取数据
                if(SocketMap.mMoreDevDataMap.containsKey(device.getDevGId())) {
                    List<Param> paramList = SocketMap.mMoreDevDataMap.get(device.getDevGId()).getParamList();
                    if (paramList != null && paramList.size() > 0) {
                        PrintUtil.print(device.getDevGId() + "：开始执行多个参数存储1");

                        saveData(device, paramList, strDate);
                        PrintUtil.print(device.getDevGId() + "：执行多个参数存储完成");
                        //清空全局变量的值
                        SocketMap.mMoreDevDataMap.remove(device.getDevGId());
                    } else {
                        if (SocketMap.mMoreDevDataMap.containsKey(device.getDevGId())) {
                            //清空全局变量的值
                            PrintUtil.print(device.getDevGId() + "：超时清空定时任务数据");
                            SocketMap.mMoreDevDataMap.remove(device.getDevGId());
                        }
                    }
                }

            }
        };
        ExecutorServiceUtil.getInstance().execute(run);
    }
    //customone定时任务
    public void c1DelayedSave(Device device, Integer size)  {

        Runnable run = new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                Thread.sleep(size * AppConf.waitCommandInterval+1000);
                //从全局变量中获取数据
                List<Param> paramList = SocketMap.c1MoreDevDataMap.get(device.getDevGId());
                if (paramList != null && paramList.size() > 0) {
                    PrintUtil.print(device.getDevGId()+"：开始执行多个参数存储");

                    /*paramList.forEach(param -> {
                        System.out.println("读取："+param.getParamName()+":"+param.getParamValue());
                    });*/

                    saveData(device, paramList, DateUtil.getStrDate());
                    PrintUtil.print(device.getDevGId()+"：执行多个参数存储完成");
                    //清空全局变量的值
                    SocketMap.c1MoreDevDataMap.remove(device.getDevGId());
                }else {
                    PrintUtil.print("全局变量数据故障:"+device.getDevGId());
                }
            }
        };

        ExecutorServiceUtil.getInstance().execute(run);
    }


    //json 数据处理方法
    public void jDataHander(String data, ChannelHandlerContext ctx) throws ParseException {

        //判断已注册中是否包含 如果包含 负责数据 心跳包 ，如果不包含  负责 注册包

        //连接的通道地址
        String address = getRemoteAddress(ctx);
        if (SocketMap.jRegPassDeviceMap != null && SocketMap.jRegPassDeviceMap.containsKey(address)) {

            ConMsg conMsg = SocketMap.jRegPassDeviceMap.get(address);

            Device device = conMsg.getDevice();
            //先判断data是不是心跳包
            if (data.equals(heart)) {
                //如果是心跳包
                PrintUtil.print("json心跳包：" + device.getDevGId() + ":" + data);

                ConHeart conHeart = conMsg.getConHeart();
                //设置心跳包收到的时间
                conHeart.setReceiveTime(new Date());
                PrintUtil.print("更新设备心跳包：" + device.getDevGId());
                if (SocketMap.deviceMap != null && SocketMap.deviceMap.containsKey(device.getDevGId())) {
                    //判断全局变量中的状态  如果全局变量中是离线更新数据库
                    Device device1 = SocketMap.deviceMap.get(device.getDevGId());
                    if (device1.getDevStatusCode().equals(AppData.offLineCode)) {
                        //更新全局变量
                        device1.setDevStatusCode(AppData.onLineCode);
                        //更新数据库设备状态
                        DevStatusUtil.handerOnlineEvent(device.getDevId());
                    }
                }

            } else {
                //先将json数据转换为对象

                MsgStatus msg = null;
                try {
                    msg = JSON.parseObject(data, MsgStatus.class);

                } catch (Exception e) {
                    //e.printStackTrace();
                    System.out.println("错误数据:" + data);
                    PrintUtil.print(device.getDevGId() + "错误的数据格式");
                    return;
                }
                //如果是数据交互
                if (EmptyUtil.isNotNull(msg.getCommandType()) && msg.getCommandType().equals("send")) {

                    //收到数据就更新心跳包
                    ConHeart conHeart = conMsg.getConHeart();
                    //设置心跳包收到的时间
                    conHeart.setReceiveTime(new Date());

                    String devGId = msg.getDevGId();
                    switch (msg.getCommand()) {

                        case "sendData": //接收数据


                            //确认信息相符合
                            if (device.getDevGId().equals(devGId)) {
                                //解析数据
                                String ss = msg.getData().toString();
                                PrintUtil.print("数据：" + devGId + ":" + data);
                                JParamData jParamData = JSON.parseObject(ss, JParamData.class);
                                List<Param> paramList = jParamData.getParamList();

                                String createTime = jParamData.getCreateTime();


                                if (!EmptyUtil.isNotNull(createTime)) {
                                    createTime = DateUtil.getStrDate();
                                }

                                //接收的数据与 设备类型的对比
                                DevJdtParamConf jdtParamConf = (DevJdtParamConf) conMsg.getTxConf();
                                //如果设备是固定参数
                                if (jdtParamConf!=null && jdtParamConf.getJdtParamConfFlag().equals("1")) {
                                    List<DevJdtParamConfData> jdtParamConfDataList = jdtParamConf.getParamConfDataList();
                                    List<Param> newParamList = new ArrayList<>();
                                    for (Param param : paramList) {
                                        for (DevJdtParamConfData jdtParamConfData : jdtParamConfDataList) {
                                            if (param.getParamName().equals(jdtParamConfData.getParamName())) {
                                                newParamList.add(param);
                                            }
                                        }
                                    }
                                    saveData(device, newParamList, createTime);
                                } else {
                                    //如果是非固定参数
                                    saveData(device, paramList, createTime);
                                }
                            }

                            break;
                        case "getDevCheckData": //获取实施数据


                            //确认信息相符合
                            if (device.getDevGId().equals(devGId)) {
                                //解析数据
                                String ss = msg.getData().toString();
                                JParamData jParamData = JSON.parseObject(ss, JParamData.class);
                                List<Param> paramList = jParamData.getParamList();

                                String createTime = jParamData.getCreateTime();


                                if (!EmptyUtil.isNotNull(createTime)) {
                                    createTime = DateUtil.getStrDate();
                                }

                                //接收的数据与 设备类型的对比
                                DevJdtParamConf jdtParamConf = (DevJdtParamConf) conMsg.getTxConf();
                                //如果设备是固定参数
                                if (jdtParamConf.getJdtParamConfFlag().equals("1")) {
                                    List<DevJdtParamConfData> jdtParamConfDataList = jdtParamConf.getParamConfDataList();
                                    List<Param> newParamList = new ArrayList<>();
                                    for (Param param : paramList) {
                                        for (DevJdtParamConfData jdtParamConfData : jdtParamConfDataList) {
                                            if (param.getParamName().equals(jdtParamConfData.getParamName())) {
                                                newParamList.add(param);
                                            }
                                        }
                                    }
                                    paramList  = newParamList;
                                }
                                if (AppData.returnMap.containsKey(device.getDevGId())) {



                                    Map<String, Object> resultMap = AppData.returnMap.get(device.getDevGId());
                                    String command = "getDevCheckData";
                                    if (resultMap.containsKey(command)) {
                                        resultMap.put(command, paramList);
                                    }
                                }


                            }

                            break;
                        case "sUploadInterval":

                            //放置到全局变量中
                            Map<String, Object> resultMap = new HashMap<>();
                            resultMap.put(msg.getCommand(), msg.getResult());
                            AppData.returnMap.put(devGId, resultMap);
                            break;
                        case "cUploadInterval": //收取设备主动更新采集频率

                            //修改数据库中的频率
                            String intervalData = msg.getData().toString();
                            if (EmptyUtil.isNotNull(intervalData)) {

                                DevCollectRateService devCollectRateService = SpringUtil.getBean("devCollectRateServiceImpl", DevCollectRateServiceImpl.class);
                                devCollectRateService.updateDevCollectRate(device.getDevId(), intervalData);

                                //更新全局变量
                                DevManager.updateDevDate(devGId);

                                //回复设备
                                msg.setResult("success");

                            } else {
                                msg.setResult("error");
                            }

                            msg.setCommandType("reply");
                            msg.setData("");


                            OutBoundHandler.outData(conMsg.getCtx(), JSON.toJSONString(msg).getBytes());

                            break;
                        case "sSetUploadData":
                            //放置到全局变量中
                            Map<String, Object> resultMap1 = new HashMap<>();
                            resultMap1.put(msg.getCommand(), msg.getResult());
                            AppData.returnMap.put(devGId, resultMap1);
                            break;
                        case "timeCalibrate":  //时间校准
                            msg.setCommandType("reply");
                            msg.setResult("success");
                            msg.setData(DateUtil.getStrDate());
                            //回复设备
                            try {
                                OutBoundHandler.outData(conMsg.getCtx(), JSON.toJSONString(msg));
                            } catch (Exception e) {

                            }
                            break;
                        case "sendDevLog": //设备上传运行日志


                            //存储数据库
                            JSONObject object = (JSONObject) JSONObject.parse(msg.getData().toString());
                            DevLog devLog = new DevLog();

                            //使用枚举进行转换
                            String sendDevEventCode = object.get("devEventTypeCode").toString();

                            DevLogEnum devLogEnum = DevLogEnum.getBySendDevEventCode(sendDevEventCode);

                            devLog.setDevLogTypeCode(devLogEnum.getDevLogTypeCode());
                            devLog.setDevEventTypeCode(devLogEnum.getMyDevEventCode());
                            devLog.setDevLogContent(object.get("devLogContent").toString());
                            devLog.setCreateTime(DateUtil.getStrDate());
                            devLog.setOperateUser(object.get("operateUser").toString());
                            devLog.setDevId(device.getDevId().toString());
                            devLog.setIsRead(1);

                            DevLogService devLogService = SpringUtil.getBean("devLogServiceImpl", DevLogServiceImpl.class);


                            devLogService.addDevLog(devLog);

                            break;

                        case "readConfig":
                        case "editConfig":
                            //放置到全局变量中
                            PrintUtil.print("收到水站数据：" + msg);
                            Map<String, Object> resultMap2 = new ConcurrentHashMap<>();
                            resultMap2.put(msg.getCommand(), data);
                            AppData.returnMap.put(devGId, resultMap2);

                            break;
                        case "getAddress":
                            //放置到全局变量中

                            Map<String, Object> resultMap3 = new ConcurrentHashMap<>();
                            resultMap3.put(msg.getCommand(), data);
                            AppData.returnMap.put(devGId, resultMap3);

                            break;


                        default:
                            break;

                    }

                }

            }

        }

    }

    //存储数据库
    public void saveData(Device device, List<Param> paramList, String createTime) {
        if(!(paramList.size() >0)){
            return;
        }
        //发给测试端数据
        StringBuffer sb2 = new StringBuffer();

        for (Param param : paramList) {
            sb2.append(param.getParamName() + ":" + param.getParamValue()  + "\n");
        }
        //测试打印数据
        if(device.getDevGId().equals("LH-DZ_313AX3788")|| device.getDevGId().equals("LH-DZ_LM21A2310001") || device.getDevGId().equals("LH-DZ_LM21A2310002")  ){
            System.out.println(device.getDevName()+"数据:"+sb2.toString());
        }


        WebSocketUtil.sendTestData(device.getDevGId(),"检测数据："+ sb2.toString());

        DevSwitchService devSwitchService = SpringUtil.getBean("devSwitchServiceImpl",DevSwitchServiceImpl.class);
        DevSwitch devSwitch = devSwitchService.selectByDevId(device.getDevId());

        if (device.getIsBind() == 1  && devSwitch.getDevSwitchValue().equals("1")) {

            //例外处理1  如果设备是LH-DZ_313AX3788  //设置时间为整点时间
            if(device.getDevGId().equals("LH-DZ_313AX3788")|| device.getDevGId().equals("LH-DZ_LM21A2310001") || device.getDevGId().equals("LH-DZ_LM21A2310002")  ){

                //补齐参数
                //获取参数列表
                DevMdtParamConfService  devMdtParamConfService = SpringUtil.getBean("devMdtParamConfServiceImpl",DevMdtParamConfServiceImpl.class);
                DevMdtParamConfDataService  devMdtParamConfDataService = SpringUtil.getBean("devMdtParamConfDataServiceImpl",DevMdtParamConfDataServiceImpl.class);
                SysDictItemService  sysDictItemService = SpringUtil.getBean("sysDictItemServiceImpl",SysDictItemServiceImpl.class);
                List<DevMdtParamConf> devMdtParamConfList = devMdtParamConfService.selectDevMdtParamConfByDevId(device.getDevId());


                //list转map
                Map<String,Param> map = new HashMap<>();
                paramList.forEach(param -> {
                    map.put(param.getParamName(),param);
                });

                devMdtParamConfList.forEach(devMdtParamConf -> {

                    List<DevMdtParamConfData> devMdtParamConfDataList = devMdtParamConfDataService.selectDevMdtParamConfDataByConfId(devMdtParamConf.getConfId());
                    devMdtParamConfDataList.forEach(devMdtParamConfData->{
                        SysDictItem sysDictItem = sysDictItemService.selectDictItemByDictCode(devMdtParamConfData.getParamCode()).get(0);
                        String paramName = sysDictItem.getDictName();
                        if(!map.containsKey(paramName)){
                            Param param = new Param();
                            param.setParamName(paramName);
                            param.setParamValue("--");
                            param.setParamUnit(sysDictItem.getDictValue());
                            param.setCheckResult(AppData.guzhangStatusCode);
                        }

                    });
                });

                //map转list
                paramList.clear();
                map.forEach((key,value)->{
                    paramList.add(value);
                });
                List<String> wuranyuanNameList = new ArrayList<>();
                //集合转成map
                Map<String,Param> paramMap = new HashMap<>();
                for(Param param : paramList){

                    //查询最新的一条数据
                    if(param.getParamName().contains("COD")){
                        wuranyuanNameList.add(param.getParamName());
                        paramMap.put(param.getParamName(),param);
                    }
                    if(param.getParamName().contains("氨氮")){
                        wuranyuanNameList.add(param.getParamName());
                        paramMap.put(param.getParamName(),param);
                    }
                    if(param.getParamName().contains("总磷")){
                        wuranyuanNameList.add(param.getParamName());
                        paramMap.put(param.getParamName(),param);
                    }
                }

                    //是否移除污染源标识
                    Integer wuranyuanFlag = 0;   //0 所有不重复   1所有重复或部分重复

                //判断与上一条数据是否相等
                CheckDataService checkDataService = SpringUtil.getBean("checkDataServiceImpl",CheckDataServiceImpl.class);
                for(String wuranyuanName : wuranyuanNameList){
                    CheckData checkData = checkDataService.selectLastParamCheck(device.getCheckDataTable(),device.getDevCheckTable(),wuranyuanName);
                   if(paramMap.containsKey(wuranyuanName)){
                       if(paramMap.get(wuranyuanName).getParamValue().equals(checkData.getCheckDataValue())){
                           wuranyuanFlag = 1;
                       }
                   }
                }
                    if(wuranyuanFlag != 0) {

                        //存储日志

                        //存储日志
                        DevLog devLog = new DevLog();
                        devLog.setDevId(device.getDevId().toString());
                        devLog.setDevLogTypeCode(AppData.devRunLogCode);
                        devLog.setDevEventTypeCode("0142");
                        StringBuilder sb = new StringBuilder();
                        for(Map.Entry<String,Param> entry : paramMap.entrySet()){
                            Param param = entry.getValue();
                            sb.append(param.getParamName()+":"+param.getParamValue());
                            paramList.removeIf(param1 -> param1.getParamName().equals(param.getParamName()));
                        }
                        String msg = "数据重复："+DateUtil.getStrDate()+":"+sb.toString();
                        devLog.setDevLogContent(msg);
                        devLog.setCreateTime(DateUtil.getStrDate());
                        devLog.setOperateUser("system");
                        devLog.setIsRead(1);
                        //存储
                        DevLogService devLogService = SpringUtil.getBean("devLogServiceImpl", DevLogServiceImpl.class);
                        devLogService.addDevLog(devLog);
                        //去除污染源数据
                        for (String ss : wuranyuanNameList) {
                            paramList.removeIf(param -> param.getParamName().equals(ss));
                        }
                    }else {
                        //存储 数据存储的日志
                        DevLog devLog = new DevLog();
                        devLog.setDevId(device.getDevId().toString());
                        devLog.setDevLogTypeCode(AppData.devRunLogCode);
                        devLog.setDevEventTypeCode("0142");
                        StringBuilder sb = new StringBuilder();
                        for(Map.Entry<String,Param> entry : paramMap.entrySet()){
                            Param param = entry.getValue();
                            sb.append(param.getParamName()+":"+param.getParamValue());
                            //paramList.removeIf(param1 -> param1.getParamName().equals(param.getParamName()));
                        }
                        String msg = "数据不重复存储："+DateUtil.getStrDate()+":"+sb.toString();
                        devLog.setDevLogContent(msg);
                        devLog.setCreateTime(DateUtil.getStrDate());
                        devLog.setOperateUser("system");
                        devLog.setIsRead(1);
                        //存储
                        DevLogService devLogService = SpringUtil.getBean("devLogServiceImpl", DevLogServiceImpl.class);
                        devLogService.addDevLog(devLog);
                        if (createTime != null) {
                            try {
                                Date nowDate = DateUtil.stringToDate(createTime, "yyyy-MM-dd HH:mm:ss");
                                createTime = DateUtil.dateToString(nowDate, "yyyy-MM-dd HH:00:00");
                            } catch (ParseException e) {
                                e.printStackTrace();
                            }
                        } else {
                            createTime = DateUtil.dateToString(new Date(), "yyyy-MM-dd HH:00:00");
                        }
                    }

            }

            if(paramList.size()==0){
                return;
            }


            //例外处理2  附加设备设备 条件触发
            if(AddSetAutoSend.addSetDevMap.size()>0 && AddSetAutoSend.addSetDevMap.containsKey(device.getDevGId())){


                AddSetMsg addSetMsg = AddSetAutoSend.addSetDevMap.get(device.getDevGId());
                AddAutoSetData addAutoSetData =addSetMsg.getAddAutoSetData();
                //条件1、 判断该设备是自动模式 1代表自动  2代表手动
                if(AddSetAutoSend.addSetDevMap.containsKey(addAutoSetData.getDevGId())
                        && AddSetAutoSend.addSetDevMap.get(addAutoSetData.getDevGId()).getNowStatus().equals(1) ) {
                    //条件2、判断界限值是否大于 如果大于 判断是否已经切换，如果未切换 开始切换设备 切换设备记录日志
                    //遍历参数
                    paramList.forEach(param -> {
                        if(param.getParamName().equals(addAutoSetData.getParamName()) && EmptyUtil.isNotNull(param.getParamValue())){

                            Double checkValue = Double.parseDouble(param.getParamValue());
                            if(checkValue >= addAutoSetData.getLimitValue()){

                                //判断当前状态是不是检测污水 如果是雨水就不进行切换  如果不是 进行切换  1雨水 2污水

                                //污水切换雨水
                                if(addSetMsg.getCheckStatus().equals(2)){
                                    new Thread(()->{
                                        try {
                                            AddSetControlUtil.switchWushuiToYushui(device.getDevGId());
                                        } catch (InterruptedException e) {
                                            e.printStackTrace();
                                        }
                                    }).start();
                                }
                            }

                        }
                    });
                }
            }


            //获取历史检测时间表格 和历史数据表格
            String devCheckTable = device.getDevCheckTable();
            String checkDataTable = device.getCheckDataTable();

            DevCheckService devCheckService = SpringUtil.getBean("devCheckServiceImpl", DevCheckServiceImpl.class);
            DevCheck devCheck = new DevCheck();
            devCheck.setDevId(device.getDevId().toString());

            if (EmptyUtil.isNotNull(createTime)) {

                devCheck.setDevCheckTime(createTime);
                devCheck.setDevCheckHour(createTime.split(" ")[1]);
            }else {
                devCheck.setDevCheckTime(DateUtil.getStrDate());
                devCheck.setDevCheckHour(DateUtil.dateToString(new Date(),"HH:mm:ss"));
            }

            devCheck.setDevCheckTable(devCheckTable);
            devCheck.setDevCheckStatusCode(AppData.checkNormalStatusCode);


            devCheckService.addDevCheck(devCheck);


            Integer flag = 0; //如果此次检测有异常 为1

            //短信 微信 通知信息
            List<Param> exceptionParamList = new ArrayList<>();



            for (Param param : paramList) {

                //添加模拟数据，目前对设备G8810_6Q84372T1 COD进行修改
                if(device.getDevGId().equals("G8810_6Q84372T1")){
                    if(param.getParamName().equals("COD")){
                        param.setParamValue(MakeDataUtil.makeCodData());
                    }
                    if(param.getParamName().equals("溶解氧")){
                        param.setParamValue(MakeDataUtil.makeRongjieyangData());
                    }
                    if(param.getParamName().equals("浊度")){
                        param.setParamValue(MakeDataUtil.makeZhuoduData());
                    }
                }
                if(device.getDevGId().equals("LHTest_000000004")){
                    if(param.getParamName().equals("水温")){
                        param.setParamValue(MakeDataUtil.makeTempData());
                    }
                }
                //添加模拟数据，目前对设备G8810_6Q84372T1 COD进行修改
                if(device.getDevGId().equals("LH-G8810_U5013704N")){
                    if(param.getParamName().equals("COD")){
                        param.setParamValue(MakeDataUtil.makeCodData2());
                    }
                    if(param.getParamName().equals("氨氮")){
                        param.setParamValue(MakeDataUtil.makeAnDanData());
                    }

                }
                //如果值为0 格式化成--
                if(device.getDevGId().equals("LH-DZ_313AX3788")|| device.getDevGId().equals("LH-DZ_LM21A2310001") || device.getDevGId().equals("LH-DZ_LM21A2310002")  ){
                    if(EmptyUtil.isNotNull(param.getParamValue())) {
                        Float floatValue = Float.parseFloat(param.getParamValue());
                        if (floatValue > 0) {

                        }else {
                            param.setParamName("--");
                        }
                    }


                }




                SysDictItemService sysDictItemService = SpringUtil.getBean("sysDictItemServiceImpl", SysDictItemServiceImpl.class);
                List<SysDictItem> sysDictItemList = sysDictItemService.selectDictItemByDictName(param.getParamName());
                if (sysDictItemList != null && sysDictItemList.size() > 0) {

                    param.setParamCode(sysDictItemList.get(0).getDictCode());

                    if (!EmptyUtil.isNotNull(param.getParamUnit())) {
                        String paramUnit = sysDictItemList.get(0).getDictValue();
                        param.setParamUnit(paramUnit);

                    }

                }

                PrintUtil.print(device.getDevGId()+":存储：" + param.getParamName() + ":" + param.getParamValue() + param.getParamUnit());
                //存储具体数据
                CheckDataService checkDataService = SpringUtil.getBean("checkDataServiceImpl", CheckDataServiceImpl.class);
                CheckData checkData = new CheckData();
                checkData.setCheckDataName(param.getParamName());
                checkData.setCheckDataValue(param.getParamValue());
                checkData.setCheckDataUnit(param.getParamUnit());
                checkData.setDevCheckId(devCheck.getDevCheckId().toString());
                checkData.setCheckDataTable(checkDataTable);

                if (param.getParamValue() != "--" && !param.getParamValue().equals("--") && EmptyUtil.isNotNull(param.getParamValue())) {


                    //判断合格标准
                    //查询合格标准
                    DevStandRangeService devStandRangeService = SpringUtil.getBean("devStandRangeServiceImpl", DevStandRangeServiceImpl.class);
                    DevStandRange devStandRange = devStandRangeService.selectDevStandRangeByDevIdAndParamCode(device.getDevId(), param.getParamCode());



                    //合格标准不为空 并且启用状态
                    if (devStandRange != null && EmptyUtil.isNotNull(devStandRange.getStandRange()) && devStandRange.getIsOpen().equals(1)) {

                        //判断是否合格
                        String result = booleanStandard(param.getParamValue(), devStandRange.getStandRange());


                        checkData.setCheckDataStatusCode(result);
                        checkData.setCheckDataDesc(devStandRange.getStandRange());

                        //存储参数异常报警


                        if (!result.equals(AppData.shiyiStatusCode)) {
                            DevReportData devReportData = new DevReportData();
                            devReportData.setDevId(device.getDevId());
                            devReportData.setParamCode(param.getParamCode());
                            devReportData.setParamValue(param.getParamValue());
                            devReportData.setParamStatusCode(result);
                            devReportData.setDevReportTime(DateUtil.getStrDate());
                            devReportData.setIsRead(1);

                            String checkResult = sysDictItemService.selectDictItemByDictCode(result).get(0).getDictName();

                            devReportData.setDevReportContent(
                                    "参数：" + param.getParamName() + ",检测值：" + param.getParamValue() + ",检测状态：" + checkResult);

                            DevReportDataService devReportDataService = SpringUtil.getBean("devReportDataServiceImpl", DevReportDataServiceImpl.class);
                            devReportDataService.addDevReportData(devReportData);



                            //添加短信内容
                            param.setCheckResult(checkResult);
                            exceptionParamList.add(param);
                           // sbMsg.append( "参数："+param.getParamName()+"("+param.getParamValue()+") 状态："+checkResult+"\n");

                            flag = 1;

                            //存储日志
                            DevLog devLog = new DevLog();
                            devLog.setDevId(device.getDevId().toString());
                            devLog.setOperateUser("system");
                            devLog.setCreateTime(DateUtil.getStrDate());
                            devLog.setDevLogContent(param.getParamName() + ",检测值：" + param.getParamValue() + ",检测状态：" + checkResult);
                            devLog.setDevLogTypeCode(AppData.devExceptionLogCode);
                            devLog.setDevEventTypeCode("0143"); //报警事件
                            devLog.setOperateUser("system");
                            devLog.setIsRead(1);

                            DevLogService devLogService = SpringUtil.getBean("devLogServiceImpl", DevLogServiceImpl.class);
                            devLogService.addDevLog(devLog);


                        }


                    } else {
                        checkData.setCheckDataStatusCode(AppData.shiyiStatusCode);
                    }

                } else {
                    checkData.setCheckDataStatusCode(AppData.guzhangStatusCode);
                    checkData.setParamStatus(null);
                }
                checkDataService.addCheckData(checkData);


            }

            //付红航定制设备 GJ004_V7YK7LDPE 自动启动关闭阀门
            if(device.getDevGId().equals("GJ004_V7YK7LDPE")){
                custom2DataEvent(paramList,device);
            }


            if (flag.equals(1)) {
                //更新异常标识
                devCheckService.updateDevCheckStatusCode(device.getDevCheckTable(), devCheck.getDevCheckId(), AppData.checkNotNormalStatusCode);
                //短信微信报警操作
                reportHander(device,exceptionParamList);
            }
        }else {
            PrintUtil.print("数据："+device.getDevGId()+"未绑定，不存储数据");
        }

        if (AppData.returnMap.containsKey(device.getDevGId())) {


            Map<String, Object> resultMap = AppData.returnMap.get(device.getDevGId());
            String command = "getDevCheckData";
            if (resultMap.containsKey(command)) {
                resultMap.put(command, paramList);
            }
        }


        //发给测试端数据
        StringBuffer sb1 = new StringBuffer();

        for (Param param : paramList) {
            sb1.append(param.getParamName() + ":" + param.getParamValue()  + "\n");
        }
        WebSocketUtil.sendTestData(device.getDevGId(),"存储数据："+sb1.toString());


    }



    //线程操作 处理报警数据
    public void reportHander(Device  dev1,List<Param> exceptionParamList){


        new Thread(()->{

            DeviceService deviceService = SpringUtil.getBean("deviceServiceImpl",DeviceServiceImpl.class);
            Device newDev = deviceService.selectDeviceByDevId(dev1.getDevId());

            //查询报警设置
            DevReportConfService devReportConfService = SpringUtil.getBean("devReportConfServiceImpl",DevReportConfServiceImpl.class);

            List<DevReportConf> devReportConfList = devReportConfService.selectDevReportConfByDevId(newDev.getDevId());



            if(devReportConfList.size()>0){

                for(DevReportConf devReportConf : devReportConfList){
                    //1、判断是否开启
                    if(devReportConf.getIsOpen().equals(1)){

                        //2、判断发送时间间隔
                        Date nowDate = new Date();
                        String sendDate = DateUtil.getStrDate();
                        Long nowTime = nowDate.getTime();
                        Long lastSendTime = 0L;
                        if(EmptyUtil.isNotNull(devReportConf.getLastSendTime())){
                            try {
                                lastSendTime = DateUtil.stringToDate(devReportConf.getLastSendTime(),"yyyy-MM-dd HH:mm:ss").getTime();
                            } catch (ParseException e) {
                                e.printStackTrace();
                            }
                        }


                        if(!EmptyUtil.isNotNull(devReportConf.getDevReportIntervalTime())){
                            return;
                        }

                        if((nowTime - lastSendTime)>=(devReportConf.getDevReportIntervalTime()*1000*60*60)){

                            //3、判断是否在发送时间范围内
                            String startTime ;
                            String endTime ;
                            if(EmptyUtil.isNotNull(devReportConf.getDevReportStartTime()) && EmptyUtil.isNotNull(devReportConf.getDevReportEndTime())){
                                startTime = DateUtil.dateToString(nowDate,"yyyy-MM-dd") +" "+devReportConf.getDevReportStartTime()+":00";
                                endTime = DateUtil.dateToString(nowDate,"yyyy-MM-dd") +" "+devReportConf.getDevReportEndTime()+":00";
                            }else {
                                startTime = DateUtil.dateToString(nowDate,"yyyy-MM-dd") +" "+"00:00:00";
                                endTime = DateUtil.dateToString(nowDate,"yyyy-MM-dd") +" "+"23:59:59";
                            }

                                Long startLongTime = DateUtil.stringToLong(startTime);
                                Long endLongTime = DateUtil.stringToLong(endTime);

                                if(nowTime >= startLongTime && nowTime<= endLongTime){

                                    //4、判断次数是不是超过限制次数
                                    //查询次数
                                    DevReportRecordService devReportRecordService = SpringUtil.getBean("devReportRecordServiceImpl",DevReportRecordServiceImpl.class);
                                    String todayStartTime = DateUtil.dateToString(new Date(),"yyyy-MM-dd")+" 00:00:00";
                                    String todayEndTime = DateUtil.dateToString(new Date(),"yyyy-MM-dd")+" 23:59:59";

                                    Integer sendCount = devReportRecordService.selectDevReportSendCount(newDev.getDevId(), devReportConf.getDevReportType(),todayStartTime,todayEndTime);



                                    if(sendCount < devReportConf.getDevReportTopLimit() ){


                                        UserService userService = SpringUtil.getBean("userServiceImpl",UserServiceImpl.class);
                                        User user = userService.selectUserByUserId(newDev.getUserId());
                                        //判读是否是短信发送
                                        if(devReportConf.getDevReportType().equals("0152")){
                                            //5、校验手机号是否存在
                                            if(EmptyUtil.isNotNull(user.getUserPhone())){



                                                //满足所有条件 开始发送
                                                //String serverMsg = "如需详细数据，请进微信小程序查询";
                                                StringBuilder sb = new StringBuilder();
                                                for(Param param : exceptionParamList){
                                                    sb.append( "参数:"+param.getParamName()+"("+param.getParamValue()+") 状态："+param.getCheckResult());
                                                }
                                                String intactShortMsg = sb.toString();



                                                //添加到数据库
                                                DevReportRecord devReportRecord = new DevReportRecord();
                                                devReportRecord.setDevId(newDev.getDevId());
                                                devReportRecord.setDevReportRecordContent(intactShortMsg);
                                                devReportRecord.setDevReportTime(sendDate);
                                                devReportRecord.setDevReportRecordType("0152");


                                                //查询是否是群发短信
                                                DevFlagConfService devFlagConfService = SpringUtil.getBean("devFlagConfServiceImpl",DevFlagConfServiceImpl.class);
                                                String devFlagConfCode = "0302";
                                                DevFlagConf devFlagConf = devFlagConfService.selectByDevIdAndCode(newDev.getDevId(),devFlagConfCode);
                                                if(devFlagConf!=null){
                                                    //查询到期时间
                                                    String expireTime = devFlagConf.getDevFlagLinkValue();
                                                    Long longExpireTime = 0L;
                                                    if(EmptyUtil.isNotNull(expireTime)) {
                                                        longExpireTime = DateUtil.stringToLong(expireTime);
                                                    }
                                                    Long currentTime = new Date().getTime();
                                                    if(currentTime < longExpireTime ){
                                                        //查询所有关联的分享用户以及自己
                                                        List<User> userList = userService.selectByDevId(newDev.getDevId());
                                                        userList.forEach(user1 -> {
                                                            //短信发送
                                                            String result =   ShortMsgUtil.sendAbnormalMsg(intactShortMsg,newDev,user1);
                                                            devReportRecord.setDevReportResult(result);
                                                            devReportRecordService.addDevReportRecord(devReportRecord);

                                                            if(result.equals("success")){

                                                                System.out.println("短信发送成功："+newDev.getDevGId());


                                                            }
                                                        });
                                                        //更新发送时间
                                                        devReportConfService.updateLastSendTime(devReportConf.getDevReportConfId(),sendDate);

                                                    }
                                                }else {
                                                    //只发送一条
                                                    User user1 = userService.selectUserByUserId(newDev.getUserId());
                                                    String result =   ShortMsgUtil.sendAbnormalMsg(intactShortMsg,newDev,user1);
                                                    devReportRecord.setDevReportResult(result);
                                                    devReportRecordService.addDevReportRecord(devReportRecord);

                                                    if(result.equals("success")){

                                                        System.out.println("短信发送成功："+newDev.getDevGId());
                                                        //更新发送时间
                                                        devReportConfService.updateLastSendTime(devReportConf.getDevReportConfId(),sendDate);

                                                    }

                                                }

                                            }


                                        }
                                       //微信发送
                                        if(devReportConf.getDevReportType().equals("0151")){



                                            //判断微信id 不能为空
                                            if(EmptyUtil.isNotNull(user.getUserWeichatId())){

                                                //组合微信数据
                                                WxMsgPack2 wxMsgPack =  getWxData(user,newDev,exceptionParamList);
                                                String result = WxMsgUtil.sendWxData(JSON.toJSONString(wxMsgPack));

                                                //添加到数据库
                                                DevReportRecord devReportRecord = new DevReportRecord();
                                                devReportRecord.setDevId(newDev.getDevId());
                                                devReportRecord.setDevReportRecordContent(wxMsgPack.getMp_template_msg().getData().getKeyword4().getValue());
                                                devReportRecord.setDevReportTime(sendDate);
                                                devReportRecord.setDevReportRecordType("0151");
                                                devReportRecord.setDevReportResult(result);
                                                devReportRecordService.addDevReportRecord(devReportRecord);

                                                if(result.equals("success")){
                                                    //更新发送时间
                                                    System.out.println("微信发送成功："+newDev.getDevGId());
                                                    devReportConfService.updateLastSendTime(devReportConf.getDevReportConfId(),sendDate);

                                                }


                                            }



                                        }
                                    }

                                }


                        }





                    }
                }
            }


        }).start();

    }



    //组合微信消息
    public WxMsgPack2 getWxData(User user, Device  dev,List<Param> exceptionParamList){

        WxMsgPack2 wxMsgPack = new WxMsgPack2();
        wxMsgPack.setTouser(user.getUserWeichatId());

        MpTemplateMsg2 mpTemplateMsg = new MpTemplateMsg2();
        mpTemplateMsg.setAppid(AppConf.mpAppId);
        mpTemplateMsg.setTemplate_id(AppConf.mpTemplate_id);
        mpTemplateMsg.setUrl(AppConf.weAppUrl);





        WxDataPack2 wxDataPack = new WxDataPack2();
        wxDataPack.setFirst(new MpFirst("您好:"+dev.getDevName()+"监测数据异常"));
        StringBuilder sb2 = new StringBuilder();
        for(Param param : exceptionParamList){

            sb2.append(param.getParamName()+":"+param.getParamValue()+","+param.getCheckResult()+";");
        }
        wxDataPack.setKeyword1(new KeyWord1(dev.getDevName(),"#173177"));
        wxDataPack.setKeyword2(new KeyWord1(DateUtil.getStrDate(),"#173177"));
        wxDataPack.setKeyword3(new KeyWord1(dev.getDevGId(),"#FF0000"));
        wxDataPack.setKeyword4(new KeyWord1(sb2.toString(),"#FF0000"));
        wxDataPack.setRemark(new MpRemark("如查看详细数据，点击进入小程序查看，如有疑问，请联系客服"));


        mpTemplateMsg.setData(wxDataPack);


        MiniProgram miniProgram = new MiniProgram();
        miniProgram.setAppid(AppConf.weAppId);
        miniProgram.setPagepath(AppConf.weAppPagePath);

        mpTemplateMsg.setMiniprogram(miniProgram);

        wxMsgPack.setMp_template_msg(mpTemplateMsg);

        return wxMsgPack;
    }

    //获取连接ctx的地址
    public String getRemoteAddress(ChannelHandlerContext ctx) {
        InetSocketAddress ipSocket = (InetSocketAddress) ctx.channel().remoteAddress();
        String clientIp = ipSocket.getAddress().getHostAddress();
        Integer port = ipSocket.getPort();
        String ip = clientIp + ":" + port;
        return ip;
    }





    //是否是json数据
    public static boolean getJSONType(String str) {
        boolean result = false;
        if (EmptyUtil.isNotNull(str)) {
            str = str.trim();
            if (str.startsWith("{") && str.endsWith("}")) {
                result = true;
            } else if (str.startsWith("[") && str.endsWith("]")) {
                result = true;
            }
        }
        return result;
    }

    //判断是否合格方法
    /*paramValue : 测量值
     * standardValue ： 标准值
     * booleanStandard 返回字典编码
     */



    public static String booleanStandard(String paramValue, String standardValue) {

        if(EmptyUtil.isNotNull(paramValue)) {

            Double intparamValue = Double.parseDouble(paramValue);
            if (standardValue.contains(">=")) {
                String strValue1 = standardValue.substring(2, standardValue.length());
                Double douValue1 = Double.parseDouble(strValue1);
                if (intparamValue >= douValue1) {
                    return AppData.shiyiStatusCode;
                } else {
                    return AppData.piandiStatusCode;
                }
            } else if (standardValue.contains("<=")) {
                String strValue2 = standardValue.substring(2, standardValue.length());
                Double douValue2 = Double.parseDouble(strValue2);
                if (intparamValue <= douValue2) {
                    return AppData.shiyiStatusCode;
                } else {
                    return AppData.piangaoStatusCode;
                }
            } else if (standardValue.contains("-") && !standardValue.equals("-")) {

                String[] scope = standardValue.split("-");
                Double[] array = {Double.parseDouble(scope[0]), Double.parseDouble(scope[1])};

                if (array[0] != null && array[1] != null) {
                    if (intparamValue >= array[0] && intparamValue <= array[1]) {
                        return AppData.shiyiStatusCode;
                    } else if (intparamValue < array[0]) {
                        return AppData.piandiStatusCode;
                    } else {
                        return AppData.piangaoStatusCode;
                    }
                } else {
                    return null;
                }
            } else {
                return AppData.shiyiStatusCode;
            }
        }else {
            return AppData.guzhangStatusCode;
        }

    }


    //获取上次检测时间
    public DevCheck getLastCheck(Device device){

        DevCheckService devCheckService = SpringUtil.getBean("devCheckServiceImpl",DevCheckServiceImpl.class);
        DevCheck devCheck = devCheckService.selectLastCheck(device.getDevCheckTable());
        return devCheck;

    }
    //获取上次检测时间
    public String getLastCheckTime(Device device){

        DevCheckService devCheckService = SpringUtil.getBean("devCheckServiceImpl",DevCheckServiceImpl.class);
        String lastCheckTime  = devCheckService.selectLastCheckTime(device.getDevCheckTable());
        return lastCheckTime;

    }

    //付红航定制设备 GJ004_V7YK7LDPE 自动启动关闭阀门
    public void custom2DataEvent(List<Param>  paramList,Device device) {

        Integer yeweiFlag = 0;  //0正常 1偏低  2偏高
        Integer dataFlag = 0;  //0正常 1异常

        for (Param param : paramList) {

            SysDictItemService sysDictItemService = SpringUtil.getBean("sysDictItemServiceImpl", SysDictItemServiceImpl.class);
            List<SysDictItem> sysDictItemList = sysDictItemService.selectDictItemByDictName(param.getParamName());
            if (sysDictItemList != null && sysDictItemList.size() > 0) {
                param.setParamCode(sysDictItemList.get(0).getDictCode());
            }

            if (param.getParamValue() != "--" && !param.getParamValue().equals("--") && EmptyUtil.isNotNull(param.getParamValue())) {
                //判断合格标准
                //查询合格标准
                DevStandRangeService devStandRangeService = SpringUtil.getBean("devStandRangeServiceImpl", DevStandRangeServiceImpl.class);
                DevStandRange devStandRange = devStandRangeService.selectDevStandRangeByDevIdAndParamCode(device.getDevId(), param.getParamCode());

                //合格标准不为空 并且启用状态
                if (devStandRange != null && EmptyUtil.isNotNull(devStandRange.getStandRange()) && devStandRange.getIsOpen().equals(1)) {

                    //判断是否合格
                    String result = booleanStandard(param.getParamValue(), devStandRange.getStandRange());


                    //存储参数异常报警
                    if (!result.equals(AppData.shiyiStatusCode)) {

                        if (param.getParamName().equals("液位")) {
                            if (result.equals(AppData.piangaoStatusCode)) {
                                yeweiFlag = 2;
                            } else {
                                yeweiFlag = 1;
                            }

                        } else {
                            dataFlag = 1;
                        }

                    }
                }
            }


        }

        //付红航定制设备 GJ004_V7YK7LDPE 自动启动关闭阀门
        //对标识进行操作
        String devGId = "GJ004_V7YK7LDPE";
        Custom2StatusService custom2StatusService = SpringUtil.getBean("custom2StatusServiceImpl",Custom2StatusServiceImpl.class);
        Custom2Status custom2Status = custom2StatusService.selectByDevId(device.getDevId());
        //液位达到上限 数据合格 打开阀门3
        if (yeweiFlag.equals(2) && dataFlag.equals(0)){
            //液位超标 并且检测值正常 需要打开阀门
            //判断当前阀门状态
            if(!custom2Status.getStatus3().equals(2)){
                //打开
                new Thread(()->{
                    Integer controlId = 15;
                    try {
                        DevLog devLog = new DevLog();
                        devLog.setDevLogTypeCode(AppData.devRunLogCode);
                        devLog.setDevEventTypeCode(AppData.devRunLogCode);
                        devLog.setCreateTime(DateUtil.getStrDate());
                        devLog.setOperateUser("system");
                        devLog.setDevId(device.getDevId().toString());
                        devLog.setIsRead(1);

                        DevLogService devLogService = SpringUtil.getBean("devLogServiceImpl", DevLogServiceImpl.class);

                        ResultStatus msg = ControlUtil.exeControlCommand(devGId,controlId);

                        if(msg.getResult().equals("success")){
                            //存储日志
                            devLog.setDevLogContent("开启阀门3成功");
                            devLogService.addDevLog(devLog);

                            Thread.sleep(2000L);

                            //然后等待2秒 立马关闭 阀门3
                            Integer closeControlId = 16;
                            ResultStatus msg1 = ControlUtil.exeControlCommand(devGId,closeControlId);

                            if(msg1.getResult().equals("success")){
                                //存储日志

                                devLog.setDevLogContent("关闭阀门3成功");
                                devLog.setCreateTime(DateUtil.getStrDate());
                                devLogService.addDevLog(devLog);
                                //改变数据库状态
                                Integer statusId = 1;
                                 Integer status3 = 2;
                                custom2StatusService.editStatus3ById(statusId,status3);

                                //更新其他阀门的状态
                                Integer status1 = 0;
                                Integer status2 = 0;
                                Integer status4 = 0;
                                custom2StatusService.editStatus1ById(statusId,status1);
                                custom2StatusService.editStatus2ById(statusId,status2);
                                custom2StatusService.editStatus4ById(statusId,status4);

                            }else {
                                devLog.setDevLogContent("关闭阀门3失败");
                                devLog.setCreateTime(DateUtil.getStrDate());
                                devLogService.addDevLog(devLog);
                            }



                        }else {
                            devLog.setDevLogContent("开启阀门3失败");
                            devLog.setCreateTime(DateUtil.getStrDate());
                            devLogService.addDevLog(devLog);
                        }


                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }).start();
            }

        }
        //液位达到上限 数据不合格 先打开阀门4 2秒后关闭  然后打开1 2秒关闭 然后打开水泵5
        if(yeweiFlag.equals(2) && dataFlag.equals(1)){
            if(!custom2Status.getStatus4().equals(2)){
                //打开
                new Thread(()->{
                    Integer controlId = 19;
                    try {
                        DevLog devLog = new DevLog();
                        devLog.setDevLogTypeCode(AppData.devRunLogCode);
                        devLog.setDevEventTypeCode(AppData.devRunLogCode);
                        devLog.setCreateTime(DateUtil.getStrDate());
                        devLog.setOperateUser("system");
                        devLog.setDevId(device.getDevId().toString());
                        devLog.setIsRead(1);
                        DevLogService devLogService = SpringUtil.getBean("devLogServiceImpl", DevLogServiceImpl.class);

                        ResultStatus msg =   ControlUtil.exeControlCommand(devGId,controlId);

                        if(msg.getResult().equals("success") ) {
                            //存储日志
                            devLog.setDevLogContent("打开阀门4成功");
                            devLogService.addDevLog(devLog);
                            //然后等待2秒  关闭阀门4
                            Integer closeControlId = 20;
                            Thread.sleep(2000L);
                            ResultStatus msg1 = ControlUtil.exeControlCommand(devGId, closeControlId);

                            if(msg1.getResult().equals("success")){

                                Integer statusId = 1;
                                 Integer status4 = 2;
                                custom2StatusService.editStatus4ById(statusId, status4);

                                devLog.setCreateTime(DateUtil.getStrDate());
                                devLog.setDevLogContent("关闭阀门4成功");
                                devLogService.addDevLog(devLog);

                                //然后打开阀门1
                                controlId = 1;

                                ResultStatus msg2 =   ControlUtil.exeControlCommand(devGId,controlId);
                                if(msg2.getResult().equals("success")){

                                    devLog.setCreateTime(DateUtil.getStrDate());
                                    devLog.setDevLogContent("打开阀门1成功");
                                    devLogService.addDevLog(devLog);
                                    controlId = 10;

                                    devLog.setCreateTime(DateUtil.getStrDate());
                                    //等待2秒钟之后关闭1
                                    Thread.sleep(2000L);
                                    ResultStatus msg3 =   ControlUtil.exeControlCommand(devGId,controlId);
                                    if(msg3.getResult().equals("success")){
                                        devLog.setCreateTime(DateUtil.getStrDate());
                                        devLog.setDevLogContent("关闭阀门1成功");
                                        devLogService.addDevLog(devLog);

                                        //打开水泵5
                                        controlId = 22;

                                        ResultStatus msg4 =   ControlUtil.exeControlCommand(devGId,controlId);
                                        if(msg4.getResult().equals("success")){
                                            devLog.setCreateTime(DateUtil.getStrDate());
                                            devLog.setDevLogContent("打开水泵成功");
                                            devLogService.addDevLog(devLog);

                                            //然后记录
                                            Integer status5 = 1;
                                            custom2StatusService.editStatus5ById(statusId, status5);


                                            //更新其他阀门的状态
                                            Integer status1 = 0;
                                            Integer status2 = 0;
                                            Integer status3 = 0;
                                            custom2StatusService.editStatus1ById(statusId,status1);
                                            custom2StatusService.editStatus2ById(statusId,status2);
                                            custom2StatusService.editStatus3ById(statusId,status3);


                                        }else {
                                            devLog.setCreateTime(DateUtil.getStrDate());
                                            devLog.setDevLogContent("打开水泵失败");
                                            devLogService.addDevLog(devLog);
                                        }


                                    }else {
                                        devLog.setDevLogContent("关闭阀门1失败");
                                        devLog.setCreateTime(DateUtil.getStrDate());
                                        devLogService.addDevLog(devLog);
                                    }

                                }else {
                                    devLog.setDevLogContent("打开阀门1失败");
                                    devLog.setCreateTime(DateUtil.getStrDate());
                                    devLogService.addDevLog(devLog);
                                }






                            }else {
                                devLog.setDevLogContent("关闭阀门4失败");
                                devLog.setCreateTime(DateUtil.getStrDate());
                                devLogService.addDevLog(devLog);
                            }

                        }else {
                            devLog.setDevLogContent("打开阀门4失败");
                            devLog.setCreateTime(DateUtil.getStrDate());
                            devLogService.addDevLog(devLog);
                        }

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }).start();

            }
        }
        //液位低于下限 5关闭  2打开2秒后关闭
        if(yeweiFlag.equals(1) ){
            //如果水泵5打开 就关闭  然后2打开 2秒后关闭
           if(custom2Status.getStatus5().equals(1))    {

                new Thread(()->{
                    Integer controlId = 23;
                    try {
                        DevLog devLog = new DevLog();
                        devLog.setDevLogTypeCode(AppData.devRunLogCode);
                        devLog.setDevEventTypeCode(AppData.devRunLogCode);
                        devLog.setCreateTime(DateUtil.getStrDate());
                        devLog.setOperateUser("system");
                        devLog.setDevId(device.getDevId().toString());
                        devLog.setIsRead(1);
                        DevLogService devLogService = SpringUtil.getBean("devLogServiceImpl", DevLogServiceImpl.class);
                        ResultStatus msg =   ControlUtil.exeControlCommand(devGId,controlId);

                        if(msg.getResult().equals("success") ){
                            //存储日志
                            devLog.setDevLogContent("水泵5关闭成功");
                            devLog.setCreateTime(DateUtil.getStrDate());
                            devLogService.addDevLog(devLog);
                            //改变数据库状态
                            Integer statusId = 1;
                            Integer status5 = 0;
                            custom2StatusService.editStatus5ById(statusId,status5);
                            //打开2
                            controlId  = 12;
                            ResultStatus msg1 =   ControlUtil.exeControlCommand(devGId,controlId);
                            if(msg1.getResult().equals("success")) {

                                devLog.setCreateTime(DateUtil.getStrDate());
                                devLog.setDevLogContent("打开阀门2成功");
                                devLogService.addDevLog(devLog);
                                Thread.sleep(2000L);
                                controlId = 13;
                                ResultStatus msg2=   ControlUtil.exeControlCommand(devGId,controlId);
                                if(msg2.getResult().equals("success")){
                                    devLog.setCreateTime(DateUtil.getStrDate());
                                    devLog.setDevLogContent("关闭阀门2成功");
                                    devLogService.addDevLog(devLog);



                                    Integer status1 = 0;
                                    Integer status2 = 0;
                                    Integer status3 = 0;
                                    Integer status4 = 0;
                                    custom2StatusService.editStatus1ById(statusId,status1);
                                    custom2StatusService.editStatus2ById(statusId,status2);
                                    custom2StatusService.editStatus3ById(statusId,status3);
                                    custom2StatusService.editStatus4ById(statusId,status4);


                                }

                            }

                        }else {
                            devLog.setDevLogContent("水泵5关闭失败");
                            devLog.setCreateTime(DateUtil.getStrDate());
                            devLogService.addDevLog(devLog);
                        }

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }).start();

            }
        }
     /*   if(yeweiFlag.equals(1) && dataFlag.equals(1) ){
            //如果数据不合格  打开阀门2
           if(custom2Status.getStatus2().equals(0))    {

                new Thread(()->{
                    Integer controlId = 12;
                    try {
                        DevLog devLog = new DevLog();
                        devLog.setDevLogTypeCode(AppData.devRunLogCode);
                        devLog.setDevEventTypeCode(AppData.devRunLogCode);
                        devLog.setCreateTime(DateUtil.getStrDate());
                        devLog.setOperateUser("system");
                        devLog.setDevId(device.getDevId().toString());
                        devLog.setIsRead(1);
                        DevLogService devLogService = SpringUtil.getBean("devLogServiceImpl", DevLogServiceImpl.class);
                        ResultStatus msg =   ControlUtil.exeControlCommand(devGId,controlId);

                        if(msg.getResult().equals("success") ){
                            //存储日志
                            devLog.setDevLogContent("打开阀门2成功");
                            devLogService.addDevLog(devLog);
                            //改变数据库状态
                            Integer statusId = 1;
                            Integer status2 = 1;
                            custom2StatusService.editStatus2ById(statusId,status2);


                            //然后等待3秒  关闭阀门
                            Thread.sleep(3000L);
                            Integer closeControlId  = 13;
                            ResultStatus msg1 =   ControlUtil.exeControlCommand(devGId,closeControlId);
                            if(msg1.getResult().equals("success")) {
                                 status2 = 0;
                                custom2StatusService.editStatus2ById(statusId, status2);
                                devLog.setDevLogContent("关闭阀门2成功");
                                devLog.setCreateTime(DateUtil.getStrDate());
                                devLogService.addDevLog(devLog);

                                //关闭水泵
                                controlId = 23;

                                ResultStatus msg2 =   ControlUtil.exeControlCommand(devGId,controlId);
                                if(msg2.getResult().equals("success")){
                                    devLog.setCreateTime(DateUtil.getStrDate());
                                    devLog.setDevLogContent("关闭水泵成功");
                                    devLogService.addDevLog(devLog);

                                    //然后记录
                                    Integer status5 = 0;
                                    custom2StatusService.editStatus5ById(statusId, status5);

                                }else {
                                    devLog.setCreateTime(DateUtil.getStrDate());
                                    devLog.setDevLogContent("关闭水泵失败");
                                    devLogService.addDevLog(devLog);
                                }

                            }else {
                                devLog.setDevLogContent("关闭阀门2失败");
                                devLog.setCreateTime(DateUtil.getStrDate());
                                devLogService.addDevLog(devLog);
                            }

                        }else {
                            devLog.setDevLogContent("打开阀门2失败");
                            devLog.setCreateTime(DateUtil.getStrDate());
                            devLogService.addDevLog(devLog);
                        }




                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }).start();

            }




        }*/
    }


}
